const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  produceImg: function(e) {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.phpUrl + 'publish/api',
      method: 'POST',
      data: {
        api_name: 'share_img',
        openid: app.globalData.userData.openOId,
        questionnaire_id: parseInt(that.data.id)
      },
      success: function(data) {
        if (data.data.code == 1) {
          wx.hideLoading();
          console.log(data.data.data.msg)
          that.setData({
            showModalDataImg: data.data.data.qrcode,
            showModalDataMsg: data.data.msg,
            showModal: true
          });
        }
      }
    })

  },

  produceArticle: function(e) {
    var that = this
    that.setData({
      showModalDataMsg: '复制以上到电脑',
      showModal2: true
    });

  },

  hideModal: function() {
    this.setData({
      showModalData: '',
      showModal: false,
      showModal2: false,
    });
  },

  showMifen: function() {
    var that = this;
    wx.request({
      url: app.globalData.phpUrl + 'publish/api',
      method: 'POST',
      data: {
        api_name: 'push',
        openid: app.globalData.userData.openOId,
        questionnaire_id: parseInt(that.data.id)
      },
      success: function(e) {
        if (e.data.code == 1) {
          wx.showToast({
            title: '申请显示成功',
            duration: 2000
          })
        };
        if (e.data.code == -1) {
          wx.showToast({
            image: "../../../images/attend.png",
            title: '申请失败',
            duration: 2000
          })
        }
      }
    })
  },

  saveImg: function(e) {
    wx.getImageInfo({
      src: e.target.dataset.img,
      success: function(result) {
        wx.saveImageToPhotosAlbum({
          filePath: result.path,
          success: function(res) {
            console.log(res);
            wx.showToast({
              title: '保存成功',
              duration: 2000
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })

      }
    })

  },

  saveText: function(e) {
    var that = this;
    wx.setClipboardData({
      data: e.target.dataset.appid + ' ' + e.target.dataset.path,
      success: function(res) {
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function(res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    });
  },

  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id || '107',
      cover: options.cover,
      title: options.title
    });
  },

  onShow: function() {

  },
  goHome: function() {
    wx.switchTab({
      url: '../../index/index'
    })
  },

  onShareAppMessage: function() {
    var that = this;
    var serName = '您的朋友';
    if (app.globalData.userInfo.nickName) {
      serName = app.globalData.userInfo.nickName
    }
    return {
      title: serName + '邀请你来答题抢红包!',
      imageUrl: '../../../images/shareImg.png',
      path: 'pages/reply/reply?id=' + that.data.id + '&article=true'
    }
  },

  review: function() {
    wx.navigateTo({
      url: '../../../../../../preview/reply?source=' + 2,
    });
  },

  onUnload:function(){
    console.log('onUnload')
    getCurrentPages()[0].setData({
      clickSubmit:false,
      isAuthorize: true,
      server_url:'',
      is_verify:1,
      imgUrl:'https://wjf.wayoxi.com/images/imgUpload.png',
      tips:0,
      useArr:[],
      banner:'',
      moldIndex:0,
      subject:{
        questionnaire_id:'',
        content:'',
        describe:'',
        banner:'',
        question:[{
          id: 0,
          content: '',
          type: 0,
          select_num:0,
          options:[
            { id: 0, content: '', type: 1, file_url: '', next_question:0},
            { id: 0, content: '', type: 1, file_url: '', next_question: 0 }
          ]
          }
        ]
      }
    })
  }
})